﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using Brjnk.Common;
using Brjnk.GpxExplorer.ViewModel;

namespace Brjnk.GpxExplorer.UI
{
    /// <summary>
    /// Interaction logic for CreateRouteWindow.xaml
    /// </summary>
    public partial class CreateRouteWindow : Window, INotifyPropertyChanged
    {
        public CreateRouteWindow(string name, IEnumerable<WaypointVM> points)
        {
            InitializeComponent();
            RouteName = name;

            Items = new ObservableCollection<CheckListItem<WaypointVM>>
                (points.Select(p => new CheckListItem<WaypointVM>(p, true)));
            CalculateDistance();
        }

        private void CalculateDistance()
        {
            double sum = 0;

            CheckListItem<WaypointVM> last = null;
            foreach (var point in items)
            {
                if (point.IsChecked)
                {
                    if (last != null)
                    {
                        sum += (last.Object.Source.Position - point.Object.Source.Position);
                    }
                    last = point;
                }
            }
            TotalDistance = sum;
        }

        private string routeName;

        public string RouteName
        {
            get { return routeName; }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new Exception("Empty name");
                routeName = value; OnPropertyChanged("RouteName");
            }
        }

        private Distance totalDistance;

        public Distance TotalDistance
        {
            get { return totalDistance; }
            private set { totalDistance = value; OnPropertyChanged("TotalDistance"); }
        }

        private ObservableCollection<CheckListItem<WaypointVM>> items;

        public ObservableCollection<CheckListItem<WaypointVM>> Items
        {
            get { return items; }
            set { items = value; OnPropertyChanged("Items"); }
        }

        private RouteVM newRoute;

        public RouteVM NewRoute
        {
            get { return newRoute; }
            set { newRoute = value; OnPropertyChanged("NewRoute"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var route = new Data.RouteDefault();
            route.Color = AutoColorSelector.GetNextColor();
            foreach (var point in items)
            {
                if (point.IsChecked)
                {
                    route.Points.Add(new Data.RoutePointDefault()
                    {
                        Position = point.Object.Source.Position,
                        Elevation = point.Object.Source.Elevation,
                        Time = point.Object.Source.Time
                    });
                }
            }
            route.Name = RouteName;
            NewRoute = new RouteVM(route);

            this.DialogResult = true;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //up
            for (int i = 0; i < items.Count; i++)
            {
                var item = items[i];
                if (item.IsSelected && i != 0 && items[i - 1].IsSelected == false)
                {
                    var temp = items[i - 1];
                    items[i - 1] = items[i];
                    items[i] = temp;
                    items[i - 1].IsSelected = true;
                    items[i].IsSelected = false;
                }
            }
            CalculateDistance();
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            //down
            for (int i = items.Count - 1; i >= 0; i--)
            {
                var item = items[i];
                if (item.IsSelected && i != items.Count - 1 && items[i + 1].IsSelected == false)
                {
                    var temp = items[i + 1];
                    items[i + 1] = items[i];
                    items[i] = temp;
                    items[i + 1].IsSelected = true;
                    items[i].IsSelected = false;
                }
            }
            CalculateDistance();
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            foreach (var item in items) item.IsChecked = true;
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            foreach (var item in items) item.IsChecked = false;
        }
    }
}